// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <string>

#include <gtest/gtest.h>
#include <pollux/core/query_config.h>
#include <optional>
#include <pollux/functions/sparksql/tests/SparkFunctionBaseTest.h>
#include <pollux/type/timestamp.h>

using namespace kumo::pollux;
using namespace kumo::pollux::exec;
using namespace kumo::pollux::functions::test;

namespace kumo::pollux::functions::sparksql::test {
class SizeTest : public SparkFunctionBaseTest {
 protected:
  std::function<vector_size_t(vector_size_t /* row */)> sizeAt =
      [](vector_size_t row) { return 1 + row % 7; };

  void testLegacySizeOfNull(VectorPtr vector, vector_size_t numRows) {
    auto result = evaluate<SimpleVector<int32_t>>(
        "size(c0, true)", make_row_vector({vector}));
    for (vector_size_t i = 0; i < numRows; ++i) {
      if (vector->is_null_at(i)) {
        EXPECT_EQ(result->value_at(i), -1) << "at " << i;
      } else {
        EXPECT_EQ(result->value_at(i), sizeAt(i)) << "at " << i;
      }
    }
  }

  void testSize(VectorPtr vector, vector_size_t numRows) {
    auto result = evaluate<SimpleVector<int32_t>>(
        "size(c0, false)", make_row_vector({vector}));
    for (vector_size_t i = 0; i < numRows; ++i) {
      EXPECT_EQ(result->is_null_at(i), vector->is_null_at(i)) << "at " << i;
    }
  }

  template <typename T>
  int32_t testArraySize(const std::vector<std::optional<T>>& input) {
    auto row = make_row_vector({make_nullable_array_vector(
        std::vector<std::vector<std::optional<T>>>{input})});
    return evaluateOnce<int32_t>("size(c0, false)", row).value();
  }

  static inline vector_size_t value_at(vector_size_t idx) {
    return idx + 1;
  }
};

// Ensure that out is set to -1 for null input if legacySizeOfNull = true.
TEST_F(SizeTest, legacySizeOfNull) {
  vector_size_t numRows = 100;
  auto array_vector =
      make_array_vector<int64_t>(numRows, sizeAt, value_at, nullptr);
  testLegacySizeOfNull(array_vector, numRows);
  array_vector =
      make_array_vector<int64_t>(numRows, sizeAt, value_at, null_every(5));
  testLegacySizeOfNull(array_vector, numRows);
  auto map_vector = make_map_vector<int64_t, int64_t>(
      numRows, sizeAt, value_at, value_at, nullptr);
  testLegacySizeOfNull(map_vector, numRows);
  map_vector = make_map_vector<int64_t, int64_t>(
      numRows, sizeAt, value_at, value_at, null_every(5));
  testLegacySizeOfNull(map_vector, numRows);
}

// Ensure that out is set to null for null input if legacySizeOfNull = false.
TEST_F(SizeTest, size) {
  vector_size_t numRows = 100;
  auto array_vector =
      make_array_vector<int64_t>(numRows, sizeAt, value_at, null_every(1));
  testSize(array_vector, numRows);
  auto map_vector = make_map_vector<int64_t, int64_t>(
      numRows, sizeAt, value_at, value_at, null_every(1));
  testSize(map_vector, numRows);
}

TEST_F(SizeTest, boolean) {
  EXPECT_EQ(testArraySize<bool>({true, false}), 2);
  EXPECT_EQ(testArraySize<bool>({true}), 1);
  EXPECT_EQ(testArraySize<bool>({}), 0);
  EXPECT_EQ(testArraySize<bool>({true, false, true, std::nullopt}), 4);
}

TEST_F(SizeTest, smallint) {
  EXPECT_EQ(testArraySize<int8_t>({}), 0);
  EXPECT_EQ(testArraySize<int8_t>({1}), 1);
  EXPECT_EQ(testArraySize<int8_t>({std::nullopt}), 1);
  EXPECT_EQ(testArraySize<int8_t>({std::nullopt, 1}), 2);
}

TEST_F(SizeTest, real) {
  EXPECT_EQ(testArraySize<float>({}), 0);
  EXPECT_EQ(testArraySize<float>({1.1}), 1);
  EXPECT_EQ(testArraySize<float>({std::nullopt}), 1);
  EXPECT_EQ(testArraySize<float>({std::nullopt, 1.1}), 2);
}

TEST_F(SizeTest, varchar) {
  EXPECT_EQ(testArraySize<std::string>({"red", "blue"}), 2);
  EXPECT_EQ(
      testArraySize<std::string>({std::nullopt, "blue", "yellow", "orange"}),
      4);
  EXPECT_EQ(testArraySize<std::string>({}), 0);
  EXPECT_EQ(testArraySize<std::string>({std::nullopt}), 1);
}

TEST_F(SizeTest, integer) {
  EXPECT_EQ(testArraySize<int32_t>({1, 2}), 2);
}

TEST_F(SizeTest, timestamp) {
  auto ts = [](int64_t micros) { return Timestamp::fromMicros(micros); };
  EXPECT_EQ(testArraySize<Timestamp>({}), 0);
  EXPECT_EQ(testArraySize<Timestamp>({std::nullopt}), 1);
  EXPECT_EQ(testArraySize<Timestamp>({ts(0), ts(1)}), 2);
}

} // namespace kumo::pollux::functions::sparksql::test
